/**
 * 股票数据处理工具模块
 */

import { calculateMA } from './stockCalculations';

/**
 * 格式化日期
 * @param {string} fmt 日期格式
 * @param {Date} date 日期对象
 * @returns {string} 格式化后的日期字符串
 */
export const formatDate = (fmt, date) => {
  const opt = {
    "Y+": date.getFullYear().toString(),
    "m+": (date.getMonth() + 1).toString(),
    "d+": date.getDate().toString(),
    "H+": date.getHours().toString(),
    "M+": date.getMinutes().toString(),
    "S+": date.getSeconds().toString(),
  };
  
  let ret;
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(
        ret[1],
        ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")
      );
    }
  }
  return fmt;
};

/**
 * 处理股票数据
 * @param {Array} data 原始数据
 * @param {number} type 数据类型
 * @param {string} name 数据名称
 * @param {number} num 数据索引
 * @param {string} type2 图表类型
 * @param {number} num3 样式索引
 * @returns {Object} 处理后的数据对象
 */
export const processStockData = (data, type, name, num, type2, num3) => {
  const result = {
    values: [],
    times: [],
    numbers: []
  };

  data.forEach((item, index) => {
    if (type === 1) {
      result.values.push(parseFloat(item[6].toFixed(2)));
      result.times.push(formatDate("YYYY-mm-dd", new Date(item[9] * 1000)));
      result.numbers.push(parseFloat(item[1]));
    } else if (type === 2) {
      const parts = item.split(",");
      result.values.push(parseFloat(parts[8]));
      result.times.push(parts[0]);
      
      if (num === 6) {
        result.numbers.push(parseFloat((parseInt(parts[num]) / 100000000).toFixed(2)));
      } else {
        result.numbers.push(parseFloat(parts[num]));
      }
    } else if (type === 3) {
      result.values.push(index + 1);
      result.times.push(formatDate("YYYY-mm-dd", new Date(item[9] * 1000)));
      result.numbers.push(parseFloat(item[1]));
    } else if (type === 4) {
      result.numbers.push(parseFloat((parseInt(item.split(",")[num]) / 100000000).toFixed(2)));
    } else if (type === 5) {
      result.numbers.push(parseFloat(item.split(",")[num]));
    }
  });

  return {
    name,
    num: result.numbers,
    time: result.times,
    array: result.values,
    type_echarts: type2,
    itemStyle: num3
  };
};

/**
 * 计算移动平均线数据
 * @param {Array} klines K线数据
 * @returns {Object} 移动平均线数据
 */
export const calculateMovingAverages = (klines) => {
  return {
    ma5: calculateMA(5, klines),
    ma10: calculateMA(10, klines),
    ma20: calculateMA(20, klines),
    ma30: calculateMA(30, klines),
    ma60: calculateMA(60, klines)
  };
};

/**
 * 计算止损位
 * @param {Array} array_all 所有数据
 * @param {Array} data1 原始数据
 * @param {Array} data2 额外数据
 * @param {string} input_name6 输入名称
 * @param {Array} time1 时间数据
 * @returns {Object} 止损计算结果
 */
export const calculateStopLoss = (array_all, data1, data2, input_name6, time1) => {
  const mas = calculateMovingAverages(array_all[0].klines);
  
  const maxData = processStockData(data1, 2, "最高值", 3).num;
  const minData = processStockData(data1, 2, "最低值", 4).num;
  const currentData = processStockData(data1, 2, "当前值", 2).num;
  
  const endTime = input_name6 || time1[time1.length - 1];
  const startTime = time1[time1.indexOf(endTime) - 20];
  
  const startIndex = time1.indexOf(startTime) + 1;
  const endIndex = time1.indexOf(endTime) + 1;
  
  const maxValue = calculateAverage(maxData, startIndex, endIndex);
  const minValue = calculateAverage(minData, startIndex, endIndex);
  const currentValue = calculateAverage(currentData, startIndex, endIndex);
  
  const historicalData = calculateHistoricalData(maxData, minData, currentData, startIndex, endIndex, time1.length);
  
  return {
    this_data: time1,
    max_data_array: maxValue,
    min_data_array: minValue,
    this_data_array: currentValue,
    ...mas,
    this_data_num: currentData,
    max_data_num: maxData,
    min_data_num: minData,
    array_data: [
      mas.ma5[mas.ma5.length - 1],
      mas.ma10[mas.ma10.length - 1],
      mas.ma20[mas.ma20.length - 1],
      mas.ma30[mas.ma30.length - 1],
      mas.ma60[mas.ma60.length - 1],
      currentData[currentData.length - 1]
    ],
    array_end: data1,
    deal_data2: historicalData
  };
}; 